package com.ryder.auth.application.controller;

import com.alibaba.fastjson2.JSON;
import com.google.common.base.Preconditions;
import com.ryder.auth.application.convert.AuthPermissionDTOConvert;
import com.ryder.auth.application.dto.AuthPermissionDTO;
import com.ryder.auth.common.entity.Result;
import com.ryder.auth.domain.entity.AuthPermissionBO;
import com.ryder.auth.domain.service.AuthPermissionDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 描述: 权限 controller
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0 // 版本号，指明从哪个版本开始
 * 更新日期: 2025/02/17
 */
@RestController
@RequestMapping("/permission/")
@Slf4j
public class PermissionController {

    @Autowired
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * 权限添加
     */
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.add.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            checkPermissionInfo(authPermissionDTO);
            Preconditions.checkNotNull(authPermissionDTO.getStatus(), "状态路由不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConvert.INSTANCE.convertDtoToBo(authPermissionDTO);
            return Result.ok(authPermissionDomainService.add(authPermissionBO));
        } catch(Exception e) {
            log.error("PermissionController.add.error: {}", e.getMessage(), e);
            return Result.fail("添加权限信息失败");
        }
    }

    /**
     * 更新权限信息
     */
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.update.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConvert.INSTANCE.convertDtoToBo(authPermissionDTO);
            return Result.ok(authPermissionDomainService.update(authPermissionBO));
        } catch(Exception e) {
            log.error("PermissionController.update.error: {}", e.getMessage(), e);
            return Result.fail("更新权限信息失败");
        }
    }

    /**
     * 删除权限
     */
    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.delete.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(), "权限id不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConvert.INSTANCE.convertDtoToBo(authPermissionDTO);
            return Result.ok(authPermissionDomainService.delete(authPermissionBO));
        } catch(Exception e) {
            log.error("PermissionController.delete.error: {}", e.getMessage(), e);
            return Result.fail("删除权限信息失败");
        }
    }

    /**
     * 查询用户权限
     */
    @GetMapping("getPermission")
    public Result<Boolean> getPermission(@RequestParam("userName") String userName) {
        try {
            if (log.isInfoEnabled()) {
                log.info("PermissionController.getPermission.userName:{}", userName);
            }
            Preconditions.checkArgument(StringUtils.isNotBlank(userName), "用户名不能为空");

            return Result.ok(authPermissionDomainService.getPermission(userName));
        } catch(Exception e) {
            log.error("PermissionController.getPermission.error: {}", e.getMessage(), e);
            return Result.fail("查询用户权限信息失败");
        }
    }

    private void checkPermissionInfo(@RequestBody AuthPermissionDTO authPermissionDTO) {
        Preconditions.checkArgument(StringUtils.isNotBlank(authPermissionDTO.getName()), "权限名称不能为空");
        Preconditions.checkNotNull(authPermissionDTO.getParentId(), "父ID不能为空");
        Preconditions.checkNotNull(authPermissionDTO.getType(), "权限类型不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(authPermissionDTO.getMenuUrl()), "菜单路由不能为空");
        Preconditions.checkNotNull(authPermissionDTO.getShow(), "展示状态不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(authPermissionDTO.getIcon()), "图标不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(authPermissionDTO.getPermissionKey()), "权限唯一标识不能为空");
    }
}
